const models = require('../db')
const express = require('express')
const router = express.Router()
const mysql = require('mysql')
const $sql = require('../sqlMap')
// 连接数据库
const conn = mysql.createConnection(models.mysql_config)
// const conn = mysql.createConnection(models.handleDisconnection)
// handleDisconnection()
// const conn = models.handleDisconnection
// conn.content
// conn.connect()
const jsonWrite = function (res, ret) {
  if (typeof ret === 'undefined') {
    res.json({
      code: '1',
      msg: '操作失败'
    })
  } else {
    res.json({
      "code": '200',
      "msg": 'ok'
    })
  }
}
const jsonWriteShow = function (res, ret) {
  if (typeof ret === 'undefined') {
    res.json({
      code: '1',
      msg: '操作失败'
    })
  } else {
    res.json(
      ret
    )
  }
}
const userinfoShow = function (res, ret) {
  console.log(ret)
  if (ret.length === 0) {

    res.json({
      code: '301',
      msg: '用户名或密码错误'
    })
  } else {
    res.json({
        code: '200',
        msg: 'ok',
        ret
      }

    )
  }
}
// 接口：增加信息
router.post('/addUserinfo', (req, res) => {
  const sql = $sql.UserInfo.add
  const params = req.query
  const jurisdiction = params.jurisdiction ? params.jurisdiction : 0
  const user_url = params.user_url ? params.user_url : 'https://portrait.gitee.com/uploads/avatars/user/7/23353_lztkdr_1578915481.png!avatar60'
  console.log('添加', params)
  conn.query(sql, [params.username, params.password, jurisdiction,user_url], function (err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWrite(res, result)
    }
  })
  conn.close;
})

// 接口：查詢信息
router.get('/showUserinfo', (req, res) => {
  const sql = $sql.UserInfo.show
  const params = req.query
  console.log('查询', params)
  conn.query(sql, [params.username, params.password], function (err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      userinfoShow(res, result)
    }
  })
  conn.close;
})
router.get('/showUserinfos', (req, res) => {
  const sql = $sql.UserInfo.showall
  const params = req.query
  console.log('查询', params)
  conn.query(sql, function (err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWriteShow(res, result)
    }
  })
  conn.close;
})

// 接口：删除信息
router.delete('/deleteUserinfo', (req, res) => {
  const sql = $sql.UserInfo.del
  const params = req.query
  console.log('删除', req)
  conn.query(sql, [params.id], function (err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWrite(res, result)
    }
  })
  conn.close;
})

// 更新信息
router.post('/updateUserInfo', (req, res) => {
  const sql = $sql.UserInfo.update
  const params = req.query
  console.log('更新', params)
  const jurisdiction = params.jurisdiction ? params.jurisdiction : 0
  const password = params.password ? params.password : ''
  const username = params.username ? params.username : ''
  const user_name = params.user_name ? params.user_name : ''
  const user_title = params.user_title ? params.user_title : ''
  const user_url = params.user_url ? params.user_url : 'https://portrait.gitee.com/uploads/avatars/user/7/23353_lztkdr_1578915481.png!avatar60'
  conn.query(sql, [username, password, jurisdiction, user_name, user_title, user_url, params.id], function (err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWrite(res, result)
    }
  })
  conn.close;
})


module.exports = router
